package com.noocodecommit.wicket.jquery.plugin.ui.progressbar;

import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.model.Model;

import com.noocodecommit.wicket.jquery.TemplatePage;
import com.noocodecommit.wicket.stuff.jquery.JQCore;
import com.noocodecommit.wicket.stuff.jquery.behavior.AbstractChainableJQBehavior;
import com.noocodecommit.wicket.stuff.jquery.plugin.chili.JQChili;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.progressbar.JQProgressBar;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.progressbar.JQProgressBarUtils;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.tabs.JQTabs;

public class ProgressBarPage extends TemplatePage
{

	private static final long serialVersionUID = 1L;
	private final JQTabs tabContainer;

	public ProgressBarPage()
	{
		super();
		JQCore.init(this);
		this.setupChili();

		tabContainer = new JQTabs("tab-container");
		tabContainer.setOutputMarkupId(true);
		add(tabContainer);

		final JQProgressBar progress0 = JQProgressBarUtils.create("progress0").interval(2000).build();
		tabContainer.add(progress0);
		Button button0Start = new Button("button0-start");
		tabContainer.add(button0Start);

		button0Start.add(new AjaxEventBehavior("onclick")
		{
			protected void onEvent(AjaxRequestTarget target)
			{
				progress0.start(target);
			}
		});

		/**
		 * 
		 */

		final JQProgressBar progress1 = JQProgressBarUtils.create("progress1").build();
		tabContainer.add(progress1);

		final Label output1 = new Label("output1");
		output1.setOutputMarkupId(true);
		tabContainer.add(output1);

		final Button button1Start = new Button("button1-start");
		tabContainer.add(button1Start);

		// TASK - STEP 1
		final AbstractChainableJQBehavior task1 = new AbstractChainableJQBehavior()
		{

			@Override
			protected void respond(AjaxRequestTarget arg0)
			{
				try
				{
					Thread.sleep(1000);
				}
				catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}

			@Override
			protected String getSuccessJavaScript()
			{
				return "$('#" + progress1.getMarkupId() + "').progressbar('progress', " + 25 + ");";
			}
		};
		button1Start.add(task1);

		// TASK - STEP 2
		final AbstractChainableJQBehavior task2 = new AbstractChainableJQBehavior()
		{

			@Override
			protected void respond(AjaxRequestTarget arg0)
			{
				try
				{
					Thread.sleep(2500);
				}
				catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}

			@Override
			protected String getSuccessJavaScript()
			{
				return "$('#" + progress1.getMarkupId() + "').progressbar('progress', " + 50 + ");";
			}
		};
		button1Start.add(task2);

		// TASK - STEP 3
		final AbstractChainableJQBehavior task3 = new AbstractChainableJQBehavior()
		{

			@Override
			protected void respond(AjaxRequestTarget arg0)
			{
				try
				{
					Thread.sleep(800);
				}
				catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}

			@Override
			protected String getSuccessJavaScript()
			{
				return "$('#" + progress1.getMarkupId() + "').progressbar('progress', " + 75 + ");";
			}
		};
		button1Start.add(task3);

		// TASK - STEP 4
		final AbstractChainableJQBehavior task4 = new AbstractChainableJQBehavior()
		{

			@Override
			protected void respond(AjaxRequestTarget target)
			{
				try
				{
					Thread.sleep(1000);
					output1.setModel(new Model("Background task is done"));
					target.addComponent(output1);
				}
				catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}

			@Override
			protected String getSuccessJavaScript()
			{
				return "$('#" + progress1.getMarkupId() + "').progressbar('progress', " + 100 + ");";
			}
		};
		button1Start.add(task4);

		task1.setSuccessChainBehavior(task2);
		task2.setSuccessChainBehavior(task3);
		task3.setSuccessChainBehavior(task4);

		button1Start.add(new AjaxEventBehavior("onclick")
		{
			protected void onEvent(AjaxRequestTarget target)
			{
				output1.setModel(new Model("Starting background task"));
				target.addComponent(output1);
				target.appendJavascript(task1.getCallbackScript().toString());
			}
		});

		JQChili chili1 = new JQChili("chili1");
		add(chili1);
		this.bindChiliCode(chili1, this.getClass(), "html");

		JQChili chili2 = new JQChili("chili2");
		add(chili2);
		this.bindChiliCode(chili2, this.getClass(), "java");

		JQCore.get().bindOnDocumentReadyFunction();
	}
}
